//引入配置数据
import { userConfig } from "../../blog-config";
const usersId = userConfig.usersId;

//引入axios
import axios from 'axios';
// 设置axios的基本配置
axios.defaults.baseURL = userConfig.serverPath;
axios.defaults.timeout = 3000;

/*
* 为Axios实例添加响应拦截器
* */
function interceptors(AxiosInstance){
    //响应拦截
    AxiosInstance.interceptors.response.use(
        response => {

            //获取响应信息
            if (response.data.code == 500){
                //如果请求出错
                console.log("请求错误！" + response.data.msg);
                return Promise.reject(response.data.msg)
            }else {
                //请求成功
                return Promise.resolve(response.data.data);
            }

        }, error => {
            return Promise.reject(error);
        });
}

/**
 * 获取用户信息
 * @param config
 * @returns {AxiosPromise}
 */
export function requestUsersInfo() {
    //创建一个axios实例
    let axiosInstance = axios.create({
        url: 'users/info/' + usersId,
        method: 'get'
    });

    //添加拦截器
    interceptors(axiosInstance);
    //返回实例
    return axiosInstance({});
}

/**
 * 查询所有的文章，分页
 */
export function requsetArticleList(pageNum, pageSize) {

    let params = {
        'pageNum': pageNum,
        'pageSize': pageSize,
        'userId': usersId,
    }
    let axiosInstance = axios.create({
        url: "article/listbyuser",
        method: 'post'
    })
    //添加拦截器
    interceptors(axiosInstance);
    return axiosInstance({params});
}

/**
 * 获取分类与文章
 * @param config
 * @returns {AxiosPromise}
 */
export function requestClassAndArticle() {
    //创建一个axios实例
    let axiosInstance = axios.create({
        url: 'classification/listArticleAndClass/' + usersId,
        method: 'get'
    });

    //添加拦截器
    interceptors(axiosInstance);
    //返回实例
    return axiosInstance({});
}

/**
 * 获取文章详情
 * @param config
 * @returns {AxiosPromise}
 */
export function requestArticleDetail(articleId) {
    //创建一个axios实例
    let axiosInstance = axios.create({
        url: 'article/infoDetail/' + usersId + "/" + articleId,
        method: 'get'
    });

    //添加拦截器
    interceptors(axiosInstance);
    //返回实例
    return axiosInstance({});
}

/**
 * 查询排名前三的文章
 * @returns {AxiosPromise}
 */
export function requsetMoreArticleList() {

    let axiosInstance = axios.create({
        url: "article//info/morehit/"+ usersId,
        method: 'get'
    })
    //添加拦截器
    interceptors(axiosInstance);
    return axiosInstance({});
}

/**
 *  根据字段进行查询
 * @param pageNum 当前第几页
 * @param pageSize 页数大小
 * @param keywords 搜索字段
 * @returns {AxiosPromise}
 */
export function requsetArticleSearchList(pageNum, pageSize, keywords) {
    let params = {
        'pageNum': pageNum,
        'pageSize': pageSize,
        'userId': usersId,
        'keywords': keywords
    }
    let axiosInstance = axios.create({
        url: "article/listbykeywords",
        method: 'get'
    })
    //添加拦截器
    interceptors(axiosInstance);
    return axiosInstance({params});
}

/**
 * 获取个人简介
 * @returns {AxiosPromise}
 */
export function requestIntroduce() {

    let axiosInstance = axios.create({
        url: "article/infoIntroduce/" + usersId,
        method: 'get'
    });
    //添加拦截器
    interceptors(axiosInstance);
    return axiosInstance({});

}